package com.example.hostsys.controller;

import com.example.hostsys.dao.HostDao;
import com.example.hostsys.dao.HouseDao;
import com.example.hostsys.dao.HousePicDao;
import com.example.hostsys.dao.MeetDao;
import com.example.hostsys.entity.*;
import com.example.hostsys.payloads.AddPicPay;
import com.example.hostsys.service.UploadService;
import com.example.hostsys.service.UserService;
import com.example.hostsys.util.ResultSet;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/host")
public class HostController {
    private String pre = "host";

    final UploadService uploadService;
    final UserService userService;
    @Autowired
    HouseDao houseDao;
    @Autowired
    HostDao hostDao;
    @Autowired
    HousePicDao housePicDao;
    @Autowired
    MeetDao meetDao;

    @Autowired
    public HostController(UploadService uploadService,UserService userService){
        this.uploadService=uploadService;
        this.userService=userService;
    }

    @GetMapping("/home")
    public String home(){
        return pre + "/home";
    }
    @GetMapping("/send")
    public String send(){
        return pre + "/send";
    }

    @ApiOperation(value = "获取上传凭证", notes = "", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hostMan", value = "hostMan", paramType = "json", required = true, dataType = "json")
    })
    @PostMapping("/getTK")
    @ResponseBody
    public ResultSet getTK(String mime, String house, Authentication authentication) {
        String tk=uploadService.getTK(mime,house,authentication.getName());
        return ResultSet.build(tk);
    }

    @PostMapping("/getCred")
    @ResponseBody
    public ResultSet getCred(Authentication auth){
        Map<String,Object> cred=uploadService.getCred(auth.getName());
        return ResultSet.build(cred);
    }

    @PostMapping("/addHouse")
    @ResponseBody
    public ResultSet addHouse(@RequestBody House house, Authentication auth){
        System.out.println(house);
        User user=userService.findUser(auth.getName());
        HostMan host=hostDao.findByUser(user);
        house.setAvailable(true);
        house.setHostMan(host);
        houseDao.save(house);
        return ResultSet.build(house.getId());
    }
    @GetMapping("/gethouse")
    @ResponseBody
    public ResultSet getHouse(Authentication auth){
        User user=userService.findUser(auth.getName());
        HostMan host=hostDao.findByUser(user);
        List<House> houses=houseDao.findAllByHostMan(host);
        for(House house:houses){
            house.sortPics();
        }
        return ResultSet.build(houses);
    }
    @PostMapping("/addPic")
    @ResponseBody
    public ResultSet addPic(@RequestBody AddPicPay addPicPay, Authentication auth){
        Integer houseID=addPicPay.getHouseID();
        String url=addPicPay.getUrl();
        House house=houseDao.findById(houseID).get();
        HousePics housePic=new HousePics(addPicPay);
        housePic.setHouse(house);
        housePicDao.save(housePic);
        return ResultSet.success();
    }
    @PostMapping("/getMeet")
    @ResponseBody
    public ResultSet getMeet(Authentication auth){
        User user=userService.findUser(auth.getName());
        HostMan host=hostDao.findByUser(user);
        List<House> houses=houseDao.findAllByHostMan(host);
        List<Meeting> res=new LinkedList<>();
        for(House house:houses){
            List<Meeting> meetings=meetDao.findAllByHouse(house);
            res.addAll(meetings);
        }
        res.sort((o2, o1) -> o1.getUpdateTime().compareTo(o2.getUpdateTime()));
        return ResultSet.build(res);
    }
}
